package com.lizard.schedule.manager.umeng;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.lizard.schedule.manager.qq.QQConst;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;

import java.util.Map;
import java.util.Set;

/**
 * Created by wangyiqian on 15/12/15.
 */
public class UmengLoginManager {

    private static UmengLoginManager instance;

    private UMSocialService controller;

    private UmengLoginManager(){
       this.controller = UMServiceFactory.getUMSocialService("com.umeng.login");
    }

    public static UmengLoginManager getInstance(){
        if(instance == null){
            synchronized (UmengLoginManager.class){
                if(instance == null){
                    instance = new UmengLoginManager();
                }
            }
        }
        return instance;
    }

    /**
     * QQ sso登录
     *
     * @param activity
     * @param listener
     */
    public void doQqSsoLogin(Activity activity, SocializeListeners.UMAuthListener listener){
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity, QQConst.APP_ID,QQConst.APP_KEY);
        qqSsoHandler.addToSocialSDK();

        controller.doOauthVerify(activity, SHARE_MEDIA.QQ, listener);
    }

    /**
     * 新浪微博sso登录
     *
     * @param activity
     * @param listener
     */
    public void doSinaSsoLogin(Activity activity, SocializeListeners.UMAuthListener listener){

        //设置新浪SSO handler
        controller.getConfig().setSsoHandler(new SinaSsoHandler());

        controller.doOauthVerify(activity, SHARE_MEDIA.SINA, listener);
    }

    /**
     * 新浪微博SSO授权时需要在相应的activity中的OnActivityResult里调用
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void doSinaOnActivityResult(int requestCode, int resultCode, Intent data){
        /**使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = controller.getConfig().getSsoHandler(requestCode);
        if(ssoHandler != null){
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

}
